package controllers

import (
	"login/models"
	"time"
)

// 登录
func (c *MainController) Login() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	username := c.GetString("username")
	password := c.GetString("password")
	loginErr := models.RedisDB.Get("loginErr" + username)
	if loginErr.Val() >= "3" {
		c.Data["json"] = Res{500, "登录失败次数大于3次禁止登录", nil}
		c.ServeJSON(true)
		return
	}
	var data models.UserCc
	err := models.DB.Debug().Where("username = ?", username).Where("password = ?", password).Find(&data).Error
	if err != nil {
		//计登录失败次数
		models.RedisDB.Incr("loginErr" + username)
		//冻结2小时
		models.RedisDB.Expire("loginErr"+username, time.Hour*2)
		c.Data["json"] = Res{500, "登录失败", err.Error()}
		c.ServeJSON(true)
		return
	}
	c.Data["json"] = Res{200, "登录成功", data}
	c.ServeJSON(true)
}
